//
//  GZZWLViewController.m
//  Estate
//
//  Created by heyong on 16/6/13.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "GZZWLViewController.h"
#import "B_GZZCSViewController.h"
#import "B_GZZQYViewController.h"
#import "B_GZZXNViewController.h"

@interface GZZWLViewController ()

@end

@implementation GZZWLViewController

-(NSMutableArray *)selectedAssetURLs{
    if(_selectedAssetURLs==nil){
        _selectedAssetURLs=[NSMutableArray array];
    }
    return  _selectedAssetURLs;
}

-(NSMutableDictionary *)dataDic{
    if(_dataDic==nil){
        _dataDic=[NSMutableDictionary dictionary];
    }
    return _dataDic;
}
- (NSMutableArray *)dataArr1{
    if(_dataArr1==nil){
        _dataArr1=[NSMutableArray array];
    }
    return _dataArr1;
}

-(NSMutableArray *)dataArr2{
    if(!_dataArr2){
        _dataArr2=[NSMutableArray array];
    }
    return _dataArr2;
}

-(UITableView *)tableView{
    if(_tableView==nil){
        _tableView=[[UITableView alloc]initWithFrame:CGRECTS(0, 0, 375, 667)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [LSUtility setExtraCellLineHidde:_tableView tabHeader:NO tabFooter:NO];
        [_tableView registerClass:[H1_ImagesCell class] forCellReuseIdentifier:kCellIdentifier_H1_ImagesCell];
    }
    return _tableView;
    
}

- (UIImagePickerController *)picker{
    if(!_picker){
        _picker = [[UIImagePickerController alloc] init];
        _picker.delegate = self;
        _picker.allowsEditing = NO;//设置可编辑
        _picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    return _picker;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.00];
    [self.view addSubview:self.tableView];
    _curTweet = [[Tweet alloc] init];
    [self setData];
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(refresh:) name:@"RefreshImage_5" object:nil];
}

- (void)refresh:(id)sender{
    if(self.selectedAssetURLs!=nil){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //设置图片
            self.curTweet.selectedAssetURLs =self.selectedAssetURLs;
            NSLog(@"self.curTweet.selectedAssetURLs:%@",self.curTweet.selectedAssetURLs);
            //回到主线程来刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:3]] withRowAnimation:UITableViewRowAnimationFade];
            });
        });
    }
}

- (void)setData{
    WEAKSELF
    if(_site){
        /*
         * 抽测混泥土
         */
        
        if(_site.wl_2){
            [weakSelf.dataDic setObject:_site.wl_2 forKey:gzz_wl_2];
        }
        
        /*
         * 设计参数情况
         */
        if(_site.wl_1!=nil){
            //钢筋混凝土灌注桩(非水下混凝土)"
            NSDictionary *_dic1 =_site.wl_1[@"钢筋混凝土灌注桩(非水下混凝土)"];
            [self.dataArr1 addObject:_dic1];
            
            //钢筋混凝土灌注桩(水下混凝土)
            NSDictionary *_dic2 =_site.wl_1[@"钢筋混凝土灌注桩(水下混凝土)"];
            [self.dataArr1 addObject:_dic2];
            
            //护坡桩
            NSDictionary *_dic3 =_site.wl_1[@"护坡桩"];
            [self.dataArr1 addObject:_dic3];
            
            //CFG
            NSDictionary *_dic4 =_site.wl_1[@"CFG"];
            [self.dataArr1 addObject:_dic4];
            
            //其他
            NSDictionary *_dic5 =_site.wl_1[@"其他"];
            [self.dataArr1 addObject:_dic5];
            
            [weakSelf.dataDic setObject:_site.wl_1 forKey:@"混凝土灌注桩的设计参数情况"];
        }else{
            //默认为空 设计参数情况
            for (int i=0; i<5; i++) {
                NSMutableDictionary *dics=[NSMutableDictionary dictionary];
                [dics setObject:@"" forKey:@"柱径"];
                [dics setObject:@"" forKey:@"桩长"];
                [dics setObject:@"" forKey:@"设计砼标号"];
                [dics setObject:@"" forKey:@"单桩设计方量m3"];
                [dics setObject:@"" forKey:@"设计砼坍落度mm"];
                [self.dataArr1 addObject:dics];
            }
            
            NSMutableDictionary *subDataDic1=[NSMutableDictionary dictionary];
            [subDataDic1 setObject:self.dataArr1[0] forKey:@"钢筋混凝土灌注桩(非水下混凝土)"];
            [subDataDic1 setObject:self.dataArr1[1] forKey:@"钢筋混凝土灌注桩(水下混凝土)"];
            [subDataDic1 setObject:self.dataArr1[2] forKey:@"护坡桩"];
            [subDataDic1 setObject:self.dataArr1[3] forKey:@"CFG"];
            [subDataDic1 setObject:self.dataArr1[4] forKey:@"其他"];
            
            [weakSelf.dataDic setObject:subDataDic1 forKey:@"混凝土灌注桩的设计参数情况"];
        
        }

        /*
         *  见证取样情况
         */
        //钢筋混凝土灌注桩(非水下混凝土)"
        if(_site.wl_3!=nil){
            NSDictionary *_dic1_1 =_site.wl_3[@"标准养护"];
            [self.dataArr2 addObject:_dic1_1];
            
            //钢筋混凝土灌注桩(水下混凝土)
            NSDictionary *_dic2_1 =_site.wl_3[@"同条件养护"];
            [self.dataArr2 addObject:DEFISNUL(_dic2_1)];
            
            //护坡桩
            NSDictionary *_dic3_1 =_site.wl_3[@"其他"];
            [self.dataArr2 addObject:_dic3_1];
            
            [weakSelf.dataDic setObject:_site.wl_3 forKey:gzz_wl_3];
        }else{
            NSMutableDictionary *subDataDic2=[NSMutableDictionary dictionary];
            for (int i=0; i<3; i++) {
                NSMutableDictionary *dics=[NSMutableDictionary dictionary];
                [dics setObject:@"" forKey:@"取样组数"];
                [dics setObject:@"" forKey:@"见证取样单编号"];
                [dics setObject:@"" forKey:@"试块编号"];
                [self.dataArr2 addObject:dics];
            }
            [subDataDic2 setObject:self.dataArr2[0] forKey:@"标准养护"];
            [subDataDic2 setObject:self.dataArr2[1] forKey:@"同条件养护"];
            [subDataDic2 setObject:self.dataArr2[2] forKey:@"其他"];
            
            [weakSelf.dataDic setObject:subDataDic2 forKey:@"见证试块取样情况"];
        
        }

        self.selectedAssetURLs= [LSUtility quertFileImageType:@"RECORD_02_SIDE_03" andId:_site.recordId andFilePathArr:_site.fileArray];
        [self refresh:nil];
        
    }else{
        //默认为空 设计参数情况
        for (int i=0; i<5; i++) {
            NSMutableDictionary *dics=[NSMutableDictionary dictionary];
            [dics setObject:@"" forKey:@"柱径"];
            [dics setObject:@"" forKey:@"桩长"];
            [dics setObject:@"" forKey:@"设计砼标号"];
            [dics setObject:@"" forKey:@"单桩设计方量m3"];
            [dics setObject:@"" forKey:@"设计砼坍落度mm"];
            [self.dataArr1 addObject:dics];
        }
        
        NSMutableDictionary *subDataDic1=[NSMutableDictionary dictionary];
        [subDataDic1 setObject:self.dataArr1[0] forKey:@"钢筋混凝土灌注桩(非水下混凝土)"];
        [subDataDic1 setObject:self.dataArr1[1] forKey:@"钢筋混凝土灌注桩(水下混凝土)"];
        [subDataDic1 setObject:self.dataArr1[2] forKey:@"护坡桩"];
        [subDataDic1 setObject:self.dataArr1[3] forKey:@"CFG"];
        [subDataDic1 setObject:self.dataArr1[4] forKey:@"其他"];
        
        [weakSelf.dataDic setObject:subDataDic1 forKey:@"混凝土灌注桩的设计参数情况"];
        
        //默认为空 见证取样情况
        NSMutableDictionary *subDataDic2=[NSMutableDictionary dictionary];
        for (int i=0; i<3; i++) {
            NSMutableDictionary *dics=[NSMutableDictionary dictionary];
            [dics setObject:@"" forKey:@"取样组数"];
            [dics setObject:@"" forKey:@"见证取样单编号"];
            [dics setObject:@"" forKey:@"试块编号"];
            [self.dataArr2 addObject:dics];
        }
        [subDataDic2 setObject:self.dataArr2[0] forKey:@"标准养护"];
        [subDataDic2 setObject:self.dataArr2[1] forKey:@"同条件养护"];
        [subDataDic2 setObject:self.dataArr2[2] forKey:@"其他"];
        [weakSelf.dataDic setObject:subDataDic2 forKey:gzz_wl_3];
    }
    
}



#pragma mark-UITaleiViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 0;
    }else{
        return 10;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==3) {
        return  [H1_ImagesCell cellHeightWithObj:_curTweet title:@"附件"];
    }else{
        return 44;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        long section=indexPath.section;
    if(section!=3){
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"aa"];
        if(cell==nil){
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aa"];
        }
        cell.textLabel.font=[UIFont systemFontOfSize:14.];
        cell.textLabel.textColor=UIColorFromRGB(0x797979);
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        
        if(section==0){
            cell.textLabel.text=@"混凝土灌注桩的设计参数情况";
        }else if(section==1){
            cell.textLabel.text=@"抽测混凝土工作性能（坍落度)情况";
        }else if(section==2){
            cell.textLabel.text=@"见证试块取样情况";
        }
        return cell;
    }else{
        WEAKSELF
        H1_ImagesCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_H1_ImagesCell forIndexPath:indexPath];
        cell.isEdit=self.isEdit;

        [cell setTitle:@"附件"];
        cell.curTweet = _curTweet;
        cell.addPicturesBlock = ^(){
            [weakSelf showActionForPhoto];
        };
        __block H1_ImagesCell *cellblock = cell;
        cell.deleteTweetImageBlockUrl = ^(TweetImage *toDelete,NSString *deurl){
            [cellblock.curTweet.tweetImages removeObject:toDelete];
            [weakSelf.selectedAssetURLs removeObject:deurl];
            [weakSelf refresh:nil];
        };
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        return cell;
        
    }
    
}

//!!!!:点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    NSLog(@"section:%li,row:%li",indexPath.section,indexPath.row);
    long section=indexPath.section;
    if(section==0){
        B_GZZCSViewController *csCtr=[[B_GZZCSViewController alloc]init];
        csCtr.isEdit=self.isEdit;
        //传入数据数组
        csCtr.dataArr=self.dataArr1;
        //回调得到修改 数据模型
        csCtr.dataBlock=^(NSMutableArray *arr){
            NSMutableDictionary *subDataDic=[NSMutableDictionary dictionary];
            [subDataDic setObject:arr[0] forKey:@"钢筋混凝土灌注桩(非水下混凝土)"];
            [subDataDic setObject:arr[1] forKey:@"钢筋混凝土灌注桩(水下混凝土)"];
            [subDataDic setObject:arr[2] forKey:@"护坡桩"];
            [subDataDic setObject:arr[3] forKey:@"CFG"];
            [subDataDic setObject:arr[4] forKey:@"其他"];
            [weakSelf.dataDic setObject:subDataDic forKey:@"混凝土灌注桩的设计参数情况"];
        };
        
        [self.navigationController pushViewController:csCtr animated:YES];
        
    }else if (section==1){
        
        B_GZZXNViewController *xnCtr=[[B_GZZXNViewController alloc]init];
        xnCtr.isEdit=self.isEdit;
        xnCtr.site=_site;
        
        xnCtr.dataBlock=^(NSDictionary *dic){
            if(_site==nil){
                //新增回填土 保存。再次进入，值不会清空
                _site=[[Site_05 alloc]init];
            }
            //得到所有key 根据判断是否显示
            for (NSString *key in [dic allKeys]) {
                [dic setValue:@"是" forKey:[NSString stringWithFormat:@"%@是否显示",key]];
            }
            _site.wl_2=dic;
            [weakSelf.dataDic setObject:dic forKey:gzz_wl_2];
        };
        
        [self.navigationController pushViewController:xnCtr animated:YES];
        
    }else if(section==2){
        
        B_GZZQYViewController *qyCtr=[[B_GZZQYViewController alloc]init];
        qyCtr.isEdit=self.isEdit;
        //传入数据数组
        qyCtr.dataArr=self.dataArr2;
        
        //回调得到修改 数据模型
        qyCtr.dataBlock=^(NSMutableArray *arr){
            
            NSMutableDictionary *subDataDic=[NSMutableDictionary dictionary];
            [subDataDic setObject:arr[0] forKey:@"标准养护"];
            [subDataDic setObject:arr[1] forKey:@"同条件养护"];
            [subDataDic setObject:arr[2] forKey:@"其他"];
            [weakSelf.dataDic setObject:subDataDic forKey:gzz_wl_3];
        };

        [self.navigationController pushViewController:qyCtr animated:YES];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark UIActionSheet M
- (void)showActionForPhoto{
    //    @weakify(self);
    [[UIActionSheet bk_actionSheetCustomWithTitle:nil buttonTitles:@[@"拍照", @"从相册选择"] destructiveTitle:nil cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
        
        [self photoActionSheet:sheet DismissWithButtonIndex:index];
        
    }] showInView:self.view];
}

- (void)photoActionSheet:(UIActionSheet *)sheet DismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        //拍照
        if (_curTweet.tweetImages.count >= 9) {//照片最多这么多
            kTipAlert(@"最多只可选择9张照片，已经选满了。先去掉一张照片再拍照呗～");
            return;
        }
  
        [self presentViewController:self.picker animated:YES completion:nil];//进入照相界面
    }else if (buttonIndex == 1){
        PictureViewController *picCtr=[[PictureViewController alloc]init];
        picCtr.flag=@"file";
        NSMutableArray *array = [self.selectedAssetURLs valueForKeyPath:@"@distinctUnionOfObjects.self"];
        picCtr.selectedImages=[[NSMutableArray alloc] initWithArray:array];
        picCtr.selectedImageBlock=^(NSMutableArray *array){
            for (NSString *path in array) {
                //添加图片路径到已选数组
                [self.selectedAssetURLs addObject:path];
            }
        };
        [self.navigationController pushViewController:picCtr animated:YES];
    }
}

#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
    self.curTweet.editImageView = [[EditImageView alloc]initWithImage:image];
    WEAKSELF
    self.curTweet.editImageView.completeBlock = ^(NSString *path) {
        //添加选择附件
        [weakSelf.curTweet addASelectedAssetURL:path];
        [weakSelf.selectedAssetURLs addObject:path];
        //刷新指定行
        [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:3]] withRowAnimation:UITableViewRowAnimationFade];
    };
    [picker dismissViewControllerAnimated:YES completion:nil];
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark scrollview
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (scrollView == self.tableView) {
        [self.view endEditing:YES];
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
